/**
 * 首页封装
 * @author hongwei 2018-08-08
 */
(function (window) {
    'use strict';
    var index = function () {
        this._init();

    };
    index.prototype = {
        _init: function () {
            var that = this;
            if (typeof(api) === 'undefined') {
                console.log('该插件只能在apicloud下运行');
            } else {
                that.delStorage();          //step1:删除storage缓存
                that.statusBar();           //step2:沉浸式状态栏处理
                that.displayApply();        //step3:是否显示申请单等
                that.slidePt();             //step4:手机端首页幻灯片
                that.getNoticeList();       //step5:获取系统公告列表
                that.getLocationIndex();    //step6:获取地址并赋值
                that.getNameFromCoords();   //step7:根据坐标查找地址获取我的坐标地址
                //下拉刷新
                push_down(function() {
                    that.delStorage();          //step1:删除storage缓存
                    that.displayApply();        //step3:是否显示申请单等
                    that.slidePt();             //step4:手机端首页幻灯片
                    that.getNoticeList();       //step5:获取系统公告列表
                    api.refreshHeaderLoadDone();
                });
            }
        },

        //step1:删除storage缓存
        delStorage: function () {
            var self = this;
            var token = $api.getStorage('token');
            var userId = $api.getStorage('user_id');
            var companyId = $api.getStorage('company_id');
            $api.clearStorage();
            //step1:存储信息
            $api.setStorage('token', token);
            $api.setStorage('user_id', userId);
            $api.setStorage('company_id', companyId);
        },

        //step2:沉浸式状态栏处理
        statusBar: function() {
            var self = this;

            //设置高度
            var header = $api.byId('header');
            $api.fixStatusBar(header);

            //滚动头部样式
            window.addEventListener("scroll", function(e) {
                var t = document.documentElement.scrollTop || document.body.scrollTop;
                if (t > 100) {
                    $(".aui-bar-nav").removeClass("test1");
                    $(".aui-bar-nav").addClass("test");
                } else {
                    $(".aui-bar-nav").removeClass("test");
                    $(".aui-bar-nav").addClass("test1");
                }
            });
        },

        //step3:是否显示申请单等
        displayApply: function() {
            var self = this;
            var param = {};
            param.url = Config.common.getCompanyInfo;
            param.data = {};
            ajax(param, function(ret) {
                //$('.aui-row .show').show();
                if (ret.apply_order == 1) {
                    $('.apply_order').show();
                }
                if (ret.expense_order == 1) {
                    $('.expense_order').show();
                    $('.allowance_order').show();
                }
            });
        },

        //step4:手机端首页幻灯片
        slidePt: function () {
            var self = this;
            var param = {};
            param.url = Config.common.getSlides;
            param.slide_id = 1;
            ajax({
                url: param.url,
                data: param
            }, function (ret) {
                if (ret.code = 1) {
                    var dataInter = ret.data;
                    var interText = doT.template($("#wap_slides").text());
                    $("#aui-slide-wrap").html(interText(dataInter));
                    var slide2 = new auiSlide({
                        container: document.getElementById("aui-slide2"),
                        // "width":300,
                        "height": 200,
                        "speed": 300,
                        "autoPlay": 5000, //自动播放
                        "pageShow": true,
                        "loop": true,
                        "pageStyle": 'dot',
                        'dotPosition': 'center'
                    });
                } else {
                    layer.msg(ret.msg);
                }
            })
        },

        //step5:获取系统公告列表
        getNoticeList: function () {
            var self = this;
            var param = {};
            param.url = Config.common.getNotice;
            param.data = {};
            ajax(param, function (ret) {
                if (ret.code == 1) {
                    var dataInter = ret.data;
                    var html = '';
                    $.each(dataInter, function (index, val) {
                        html += '<div class="swiper-slide">' + val.content + '</div>'
                    });
                    $('.swiper-wrapper').html('');
                    $('.swiper-wrapper').html(html);
                    var swiper = new Swiper('.swiper-container', {
                        direction: 'vertical',
                        slidesPerView: 1,
                        mousewheel: true,
                        loop: true,
                        pagination: {
                            el: '.swiper-pagination',
                            clickable: true,
                        },
                        autoplay: {
                            delay: 2500,
                            disableOnInteraction: false,
                        }
                    });
                } else {
                    $toast(ret.msg, 0, 'm');
                }
            })
        },
        //step6:获取地址并赋值
        getLocationIndex: function () {
            var self = this;
            var map = api.require('bMap');
            map.getLocation({
                accuracy: '100m',
                autoStop: true,
                filter: 1
            }, function (ret, err) {
                if (ret.status) {
                    // console.log(ret.lat);
                    // console.log(ret.lon);
                    self.getNameFromCoords(ret.lon, ret.lat);
                } else {
                    // console.log(err.code);
                }
            });
        },

        /**
         * step7:根据坐标查找地址获取我的坐标地址
         * @param lon 经度
         * @param lat 纬度
         */
        getNameFromCoords: function (lon, lat) {
            var self = this;
            var map = api.require('bMap');
            map.getNameFromCoords({
                lon: lon,
                lat: lat
            }, function (ret, err) {
                //console.log(JSON.stringify(ret));
                if (ret.status) {
                    //console.log(JSON.stringify(ret));
                    $('#city').text(ret.city);
                } else {
                    self.getLocationIndex(); //解决iphoneX第一次定位有问题，再刷新定位
                }
            });
        }
    };
    window.index = index;
})(window);
